package com.lz.fruitday.fragment;


import android.graphics.Bitmap;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.lz.fruitday.R;
import com.lz.fruitday.constant.Path;
import com.lz.fruitday.utils.ImageLoaderUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cz.msebera.android.httpclient.Header;

/**
 * A simple {@link Fragment} subclass.
 */
public class GardenFragment extends Fragment implements View.OnClickListener{

    @ViewInject(R.id.image)
    private ImageView image;
    @ViewInject(R.id.tv_name)
    private TextView tvName;
    @ViewInject(R.id.tv_descri)
    private TextView tvDesc;
    @ViewInject(R.id.btn_login)
    private Button btnLogin;
    @ViewInject(R.id.btn_share)
    private Button btnShare;
    private DisplayImageOptions options= ImageLoaderUtil.getDefaultOptions();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_garden, container, false);
        ViewUtils.inject(this, view);
        initView();
        return view;
    }

    private void initView() {
        btnLogin.setOnClickListener(this);
        btnShare.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                Platform platform= ShareSDK.getPlatform(SinaWeibo.NAME);
                login(platform);
                break;
            case R.id.btn_share:
                showShare();
                break;
        }
    }

    private void login(Platform platform) {
//        platform.removeAccount();
//        platform.removeAccount(true);
        if (platform==null){
            return;
        }
        PlatformDb db=platform.getDb();

        if (db.isValid()){
            String userName = db.getUserName();
            String userIcon = db.getUserIcon();
            ImageLoader.getInstance().displayImage(userIcon, image, options);
            return;
        }

        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                String userIcon = (String) hashMap.get("avatar_large");
                ImageLoader.getInstance().displayImage(userIcon, image, options, new ImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String imageUri, View view) {

                    }

                    @Override
                    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    }

                    @Override
                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                        Log.e("imageUri",imageUri);
                    }

                    @Override
                    public void onLoadingCancelled(String imageUri, View view) {

                    }
                });
                tvName.setText(hashMap.get("name").toString());
                tvDesc.setText(hashMap.get("location").toString());
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(Platform platform, int i) {

            }
        });
        platform.showUser(null);
    }

    private void showShare() {
        OnekeyShare onekeyShare=new OnekeyShare();
        onekeyShare.disableSSOWhenAuthorize();
        onekeyShare.setText("分享从这里开始");
        onekeyShare.show(getActivity());
    }
}
